<template>
  <div>
    <el-card>
      <div class="header">
        <el-button type="primary" @click="handleAdd">添加标签</el-button>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
          prop="name"
          label="标签名"
        >
        </el-table-column>
        <el-table-column
          label="操作"
        >
          <template slot-scope="scope">
            <el-button v-if="scope.row.tenantId!=='0'" size="mini" type="primary" @click="handleEdit(scope.row)">编辑
            </el-button>
            <el-button v-if="scope.row.tenantId!=='0'" size="mini" type="danger" @click="handleDelete(scope.row.id)">删除
            </el-button>
            <span v-else>系统默认选项，无法操作</span>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        layout="total, prev, pager, next"
        :total="total"
      ></el-pagination>
    </el-card>
    <create ref="create" @Ok="handleInit"></create>
    <edit ref="edit" @Ok="handleInit"></edit>
  </div>
</template>

<script>
  import {deleteTag, getTList} from "@/api/set";
  import Create from './components/create'
  import Edit from './components/edit'

  export default {
    name: "index",
    components: {
      Create,
      Edit
    },
    data() {
      return {
        page: 1,
        size: 10,
        total: 0,
        tableData: []
      }
    },
    mounted() {
      this.handleInit()
    },
    methods: {
      handleInit() {
        let form = {
          page: this.page,
          size: this.size
        };
        getTList(form).then(res => {
          this.tableData = res.result.records;
          this.total = Number(res.result.total);
        })
      },
      handleAdd() {
        this.$refs['create'].show()
      },
      handleEdit(data) {
        this.$refs['edit'].show(data)
      },
      handleDelete(id) {
        deleteTag({id: id}).then(res => {
          if (res.status === 200) {
            this.handleInit()
            this.$message.success("删除成功")
          }
        })
      },
      handleCurrentChange(val) {
        this.page = val;
        this.handleInit();
      },
    }
  }
</script>

<style scoped>
  .header {
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }
</style>
